//
//  ECIPSettingViewController.m
//  ECERP.CRM
//
//  Created by Mac on 2018/10/19.
//  Copyright © 2018年 Mac. All rights reserved.
//

#import "ECIPSettingViewController.h"
#import "ECIPSettingTableViewCell.h"
#import "ECAddCompanyViewController.h"
#import "ECCompanyModel.h"
#import "LZCacheTool.h"
#define KCompanyInfoCacheName @"companyIpInfoCacheName"  //缓存cache名字

@interface ECIPSettingViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UIButton *addBtn;

@property (nonatomic,strong)NSMutableArray *datasourceArray;

@end

@implementation ECIPSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title=@"设置";
    
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame=CGRectMake(0, 0, 14, 14);
    [btn setImage:[UIImage imageNamed:@"return_icon"] forState:UIControlStateNormal];
    [btn addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
    
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.addBtn];
    
    [self refreshTableViewAction];
    
    //接收通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(refreshTableViewAction) name:@"KCacheCompanySuccess" object:nil];
}

-(void)refreshTableViewAction
{
    NSArray *arr=  [LZCacheTool getCacheFileName:KCompanyInfoCacheName];
    self.datasourceArray=[NSMutableArray arrayWithArray:arr];
    [self.tableView reloadData];
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.datasourceArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ECIPSettingTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    ECCompanyModel *model=[self.datasourceArray objectAtIndex:indexPath.row];
    cell.nameLab.text=model.companyName;
    cell.addressLab.text=model.companyIP;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, ECSCREEN_WIDTH, 140) style:UITableViewStylePlain];
        _tableView.delegate=self;
        _tableView.dataSource=self;
        _tableView.bounces=NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ECIPSettingTableViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.rowHeight=70;
    }
    return _tableView;
}


- (UIButton *)addBtn
{
    if (!_addBtn) {
        _addBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        _addBtn.frame=CGRectMake(12, _tableView.bottom+40, ECSCREEN_WIDTH-24, 49);
        [_addBtn setTitle:@"添加公司" forState:UIControlStateNormal];
        _addBtn.titleLabel.font=ECFONT(16);
        _addBtn.backgroundColor=ECCOLOR_NAVIBACK;
        _addBtn.layer.cornerRadius=12.0;
        [_addBtn addTarget:self action:@selector(addCompanyClickAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addBtn;
}

-(void)addCompanyClickAction
{
    ECAddCompanyViewController *companyVC=[[ECAddCompanyViewController alloc]init];
    [self.navigationController pushViewController:companyVC animated:YES];
}

-(void)dealloc
{
    //移除通知
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"KCacheCompanySuccess" object:nil];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
